package com.chengqs.rapid.console.web;

import com.chengqs.rapid.common.config.Rule;
import com.chengqs.rapid.console.dto.RuleDTO;
import com.chengqs.rapid.console.service.RuleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * <B>主类名称：</B>RuleController<BR>
 * <B>概要说明：</B>规则控制层<BR>
 *
 * @author: chengqs
 * @create: 2025-03-18 18:00:28
 **/
@RestController
public class RuleController {
    @Autowired
    private RuleService ruleService;

    @RequestMapping("rule/getList")
    public List<Rule> getList(@RequestParam("prefixPath") String prefixPath) throws Exception{
        List<Rule> list = ruleService.getRuleList(prefixPath);
        return list;
    }

    @RequestMapping("rule/add")
    public void addRule(@RequestBody RuleDTO ruleDTO) throws Exception {
        if(ruleDTO != null) {
            Rule rule = new Rule();
            rule.setId(ruleDTO.getId());
            rule.setName(ruleDTO.getName());
            rule.setProtocol(rule.getProtocol());
            rule.setOrder(ruleDTO.getOrder());
            rule.setFilterConfigs(ruleDTO.getFilterConfigs());
            ruleService.addRule(ruleDTO.getPrefixPath(), rule);
        }
    }

    @RequestMapping("rule/update")
    public void updateRule(@RequestBody RuleDTO ruleDTO) throws Exception {
        if(ruleDTO != null) {
            Rule rule = new Rule();
            rule.setId(ruleDTO.getId());
            rule.setName(ruleDTO.getName());
            rule.setProtocol(rule.getProtocol());
            rule.setOrder(ruleDTO.getOrder());
            rule.setFilterConfigs(ruleDTO.getFilterConfigs());
            ruleService.updateRule(ruleDTO.getPrefixPath(), rule);
        }
    }

    @RequestMapping("rule/delete")
    public void deleteRule(@RequestBody RuleDTO ruleDTO) {
        if(ruleDTO != null) {
            ruleService.deleteRule(ruleDTO.getPrefixPath(), ruleDTO.getId());
        }
    }
}
